/**
* All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
*
* MmsPostAuthorizationInfoService
* 岗位授权信息表新
*
* 修改纪录
* 2021-06-29 版本：1.0 hudeyong 创建。
* @version 版本：1.0
* @author 作者：hudeyong
* 创建日期：2021-06-29
*/
package com.topscomm.mms.service.impl;

import cn.hutool.core.convert.Convert;
import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.exception.ServiceException;
import com.topscomm.mms.pojo.MmsDeptExtraInfoEntity;
import com.topscomm.mms.pojo.MmsPostAuthorizationInfoEntity;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.tap.common.DatacodeCache;
import com.topscomm.tap.common.RedisMmsStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.mms.service.IMmsPostAuthorizationInfoService;
import com.topscomm.mms.service.impl.auto.MmsPostAuthorizationInfoServiceAuto;
import com.topscomm.tap.common.RedisDatacodeStore;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(value = "mmsPostAuthorizationInfoService")
@Scope(value = "singleton")
public class MmsPostAuthorizationInfoService extends MmsPostAuthorizationInfoServiceAuto implements IMmsPostAuthorizationInfoService {
    /**
     * @description: 新增之前唯一性校验
     * @param entity
     * @return void
     * @author: wangxinhua
     * @date: 2021/7/15 13:56
     * @modify:
     */
    @Override
    protected void beforeInsert(BasicEntity entity) throws ServiceException {
//        MmsPostAuthorizationInfoEntity isExists = this.queryObjectByWhere(
//                MmsPostAuthorizationInfoEntity.FieldDeptcommoncode + "="+"'"+entity.getValue(MmsPostAuthorizationInfoEntity.FieldDeptcommoncode)+"' and "
//                + MmsPostAuthorizationInfoEntity.FieldPostcode + "="+"'"+entity.getValue(MmsPostAuthorizationInfoEntity.FieldPostcode)+"'"
//        );
//        if(isExists != null){
//            throw  new ServiceException("当前添加的岗位在部门中已存在！请前往已添加的岗位维护信息。");
//        }
    }
    /**
     * @description: 更新之前唯一性校验
     * @param map
     * @return void
     * @author: wangxinhua
     * @date: 2021/7/15 13:56
     * @modify:
     */
    @Override
    protected void updateBefore(Map<String, Object> map) throws ServiceException {
//        MmsPostAuthorizationInfoEntity isExists = this.queryObjectByWhere(
//                MmsPostAuthorizationInfoEntity.FieldDeptcommoncode + "="+"'"+map.get(MmsPostAuthorizationInfoEntity.FieldDeptcommoncode)+"' and "
//                        + MmsPostAuthorizationInfoEntity.FieldPostcode + "="+"'"+map.get(MmsPostAuthorizationInfoEntity.FieldPostcode)+"'"
//        );
//        if(isExists != null && !ConvertUtil.convertToString(isExists.getId()).equals(ConvertUtil.convertToString(map.get(MmsPostAuthorizationInfoEntity.FieldId)))){
//            throw  new ServiceException("当前添加的岗位在部门中已存在！请前往已添加的岗位维护信息。");
//        }
    }

    @Override
    protected void updateAfter(Map<String, Object> map) throws ServiceException {
        super.updateAfter(map);
        this.sendMessageToFds(map,MmsPostAuthorizationInfoEntity.tableName);
        this.addValueToRedis(MmsPostAuthorizationInfoEntity.tableName);
    }

    @Override
    protected void afterInsert(BasicEntity entity) throws ServiceException {
        super.afterInsert(entity);
        Map<String, Object> map = entity.convertToMap();
        this.sendMessageToFds(map,MmsPostAuthorizationInfoEntity.tableName);
        this.addValueToRedis(MmsPostAuthorizationInfoEntity.tableName);
    }

    @Override
    protected void deleteAfter(String whereSql) {
        super.deleteAfter(whereSql);
        this.addValueToRedis(MmsPostAuthorizationInfoEntity.tableName);
    }

    /**
     * @description: 获取岗位信息
     * @param paramMap
     * @return java.util.List<java.lang.String>
     * @author: wangxinhua
     * @date: 2021/7/15 19:51
     * @modify:
     */
    @Override
    public List<Map<String, Object>> getPostNameList(Map<String, Object> paramMap) {
        //获取datacodecache
        DatacodeCache datacodeCache = (DatacodeCache) SpringUtil.getBean("datacodeCache");
        //获取当前人所属省区
        String deptCommonCode = this.getDeptCommonCode(getSessionUserBean().getDept().getCode());
        // 拼接查询sql
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle("{0} =1 ",  MmsPostAuthorizationInfoEntity.FieldEnabled);
        sql.appendSingle("and (visibledeptcommoncodetype = '1' or ( visibledeptcommoncodetype = '2' and ',' || visibledeptcommoncoderange || ','  like  '%,{0},%' ))", deptCommonCode);
        if(paramMap.containsKey("productLineCode") && !StringUtil.isEmpty(ConvertUtil.convertToString(paramMap.get("productLineCode")))){
            sql.appendSingle("and (visibleproductlinetype = '1' or ( visibleproductlinetype = '2' and ',' || visibleproductlinerange || ','  like  '%,{0},%' ))", ConvertUtil.convertToString(paramMap.get("productLineCode")));
        }
        sql.appendSingle("ORDER BY TO_NUMBER({0}) DESC", MmsPostAuthorizationInfoEntity.FieldPostweight);
        List<MmsPostAuthorizationInfoEntity> postAuthorizationInfoEntities = this.queryByWhere(sql.toString());
        //组装数据
        List<Map<String, Object>> postNameList = new ArrayList<Map<String, Object>>();
        for(MmsPostAuthorizationInfoEntity postAuthorizationInfoEntity : postAuthorizationInfoEntities) {
            Map<String, Object> teMap = new HashMap<String, Object>();
            teMap.put("postcode", postAuthorizationInfoEntity.getPostcode());
            String usercode = postAuthorizationInfoEntity.getUsercode();
            String[] userCodeArray = usercode.split(",");
            String userName = postAuthorizationInfoEntity.getUsername();
            String[] userNameArray = userName.split(",");
            StringBuilder userBuffer = new StringBuilder();
            for (int i = 0; i < userCodeArray.length; i++) {
                userBuffer.append(userCodeArray[i]).append(" ").append(userNameArray[i]);
                userBuffer.append(" ");
            }

            String postBuffer1 = datacodeCache.getValueDynamic("PostCode", postAuthorizationInfoEntity.getPostcode(), "code", "name") +
                    "(" + userBuffer.toString().trim() + ")";
            teMap.put("name", postBuffer1);
            teMap.put("weight", postAuthorizationInfoEntity.getPostweight());
            teMap.put("fcode", postAuthorizationInfoEntity.getDeptcommoncode());
            teMap.put("productlines", postAuthorizationInfoEntity.getVisibleproductlinerange());
            teMap.put("whethermust", postAuthorizationInfoEntity.getWhethermust());
            teMap.put("whetherdefault", postAuthorizationInfoEntity.getWhetherdefault());
            teMap.put("enabled", postAuthorizationInfoEntity.getEnabled());
            postNameList.add(teMap);
        }
        return postNameList;
    }
}
